package com.maple.phoneward.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Pattern;

import android.app.Dialog;
import android.os.Bundle;
import android.text.Html;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;

import com.maple.phoneward.constants.Constants;
import com.maple.phoneward.custom.DialogBuilder;
import com.maple.phoneward.entity.Contacts;
import com.maple.phoneward.utils.AppUtil;
import com.maple.phoneward.utils.StringUtil;

/**
 * FirstSetActivity.java
 * 
 * @author YaJian Qiu
 * @time 2013-3-30 下午5:02:47<br>
 *       本类描述：首次开启防盗服务设置界面
 * 
 */
public class FirstSetActivity extends BaseActivity {

	private LinearLayout container;
	private Button leftButton, rightButton;
	private TextView stepTextView;
	private int step = 1;// 第几步
	private String step1;
	private String step2;
	private String step3;
	private View stepView;
	private EditText inputEditText, repeatEditText;
	private CheckBox checkBox;
	private WebView webView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.firstset);

		container = (LinearLayout) findViewById(R.id.firstset_container);
		stepTextView = (TextView) findViewById(R.id.firstset_step_text);
		leftButton = (Button) findViewById(R.id.firstset_left_button);
		rightButton = (Button) findViewById(R.id.firstset_right_button);
		step1 = "<font face='verdana' color='#ea66a6' size=20px >" + getString(R.string.step_1) + "</font><br/>" + getString(R.string.step_2) + "<br/>"
				+ getString(R.string.step_3);
		step2 = getString(R.string.step_1) + "<br/><font face='verdana' color='#ea66a6' size=20px>" + getString(R.string.step_2) + "</font><br/>" + getString(R.string.step_3);
		step3 = getString(R.string.step_1) + "<br/>" + getString(R.string.step_2) + "<br/><font face='verdana' color='#ea66a6' size=20px>" + getString(R.string.step_3) + "</font>";
		showTextAndView();
	}

	/**
	 * 显示文字提示
	 */
	private void showTextAndView() {
		switch (step) {
		case 1:
			stepTextView.setText(Html.fromHtml(step1));
			stepView = LayoutInflater.from(this).inflate(R.layout.step1, null);
			leftButton.setVisibility(View.GONE);
			rightButton.setText(R.string.next_step);
			titleView.setText(R.string.step_1_title);// 设置标题
			inputEditText = (EditText) stepView.findViewById(R.id.input_edit_text);
			repeatEditText = (EditText) stepView.findViewById(R.id.repeat_edit_text);
			checkBox = (CheckBox) stepView.findViewById(R.id.step_1_check_box);

			String pwd = getConfig(Constants.CONFIG_PHONEWARD_PASSWORD);
			inputEditText.setText(pwd);// 回显
			repeatEditText.setText(pwd);

			final int type = inputEditText.getInputType();// 129 输入类型
			checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {
						inputEditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
						repeatEditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
					} else {
						inputEditText.setInputType(type);
						repeatEditText.setInputType(type);
					}
				}
			});
			break;
		case 2:
			stepTextView.setText(Html.fromHtml(step2));
			stepView = LayoutInflater.from(this).inflate(R.layout.step2, null);
			leftButton.setVisibility(View.VISIBLE);
			rightButton.setText(R.string.next_step);
			titleView.setText(R.string.step_2_title);// 设置标题
			inputEditText = (EditText) stepView.findViewById(R.id.input_edit_text);
			repeatEditText = (EditText) stepView.findViewById(R.id.repeat_edit_text);
			String num1 = getConfig(Constants.CONFIG_SAFETY_PHONE_NUMBER_1);
			String num2 = getConfig(Constants.CONFIG_SAFETY_PHONE_NUMBER_2);
			inputEditText.setText(num1);
			repeatEditText.setText(num2);
			break;
		case 3:
			stepTextView.setText(Html.fromHtml(step3));
			stepView = LayoutInflater.from(this).inflate(R.layout.step3, null);
			leftButton.setVisibility(View.VISIBLE);
			rightButton.setText(R.string.complete);
			titleView.setText(R.string.step_3_title);// 设置标题
			inputEditText = (EditText) stepView.findViewById(R.id.input_edit_text);
			repeatEditText = (EditText) stepView.findViewById(R.id.repeat_edit_text);
			String email = getConfig(Constants.CONFIG_EMAIL);
			inputEditText.setText(email);
			repeatEditText.setText(email);
			break;

		}
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		if (container.getChildCount() > 0) {
			container.removeAllViews();
		}
		webView = (WebView) stepView.findViewById(R.id.web_view);
		webView.setBackgroundColor(0);
		webView.loadUrl("file:///android_asset/step_" + step + "_help.html");
		container.addView(stepView, params);
	}

	/**
	 * 监听上一步
	 * 
	 * @param view
	 */
	public void leftButtonListener(View view) {
		if (step == 1) {
			Button btn = (Button) view;
			btn.setVisibility(View.GONE);
		}

		step--;
		showTextAndView();
	}

	/**
	 * 监听下一步
	 * 
	 * @param view
	 */
	public void rightButtonListener(View view) {
		String input = inputEditText.getText().toString();
		String repeat = repeatEditText.getText().toString();
		switch (step) {
		case 1:
			if (!AppUtil.isPassword(input)) {
				AppUtil.showShortMessage(this, R.string.step_1_input_toast);
				return;
			}
			if (StringUtil.isEmpty(repeat)) {
				AppUtil.showShortMessage(this, R.string.step_1_not_null2);
				return;
			}
			if (input.length() < 6) {
				AppUtil.showShortMessage(this, R.string.step_1_lenght_6);
				return;
			}
			if (StringUtil.equals(input, repeat)) {
				// 保存防盗密码
				saveConfig(Constants.CONFIG_PHONEWARD_PASSWORD, input);
			} else {
				AppUtil.showShortMessage(this, R.string.step_1_not_equal);
				return;
			}
			break;
		case 2:
			if (StringUtil.isEmpty(input) && StringUtil.isEmpty(repeat)) {
				AppUtil.showShortMessage(this, R.string.step_2_hint_2);
				return;
			}
			if (!StringUtil.isEmpty(input) && !AppUtil.isPhoneNumber(input)) {
				AppUtil.showShortMessage(this, R.string.step_2_toast);
				return;
			}
			if (!StringUtil.isEmpty(repeat) && !AppUtil.isPhoneNumber(repeat)) {
				AppUtil.showShortMessage(this, R.string.step_2_toast);
				return;
			}
			if (StringUtil.equals(input, repeat)) {
				AppUtil.showShortMessage(this, R.string.step_2_not_equals);
				return;
			}
			String localNum = AppUtil.getPhoneNumber(this);// 本机号码
			if (StringUtil.equals(input, localNum) || StringUtil.equals(repeat, localNum)) {
				AppUtil.showShortMessage(this, R.string.step_2_not_self);
				return;
			}
			if (!StringUtil.isEmpty(input))
				saveConfig(Constants.CONFIG_SAFETY_PHONE_NUMBER_1, input);
			if (!StringUtil.isEmpty(repeat))
				saveConfig(Constants.CONFIG_SAFETY_PHONE_NUMBER_2, repeat);
			break;
		case 3:

			if (StringUtil.isEmpty(input)) {
				AppUtil.showShortMessage(this, R.string.step_3_not_null);
				return;
			}
			if (StringUtil.isEmpty(repeat)) {
				AppUtil.showShortMessage(this, R.string.step_3_not_null2);
				return;
			}
			if (!AppUtil.isEmail(repeat)) {
				AppUtil.showShortMessage(this, R.string.step_3_toast);
				return;
			}
			if (StringUtil.equals(input, repeat)) {
				saveConfig(Constants.CONFIG_EMAIL, input);
				this.setResult(RESULT_OK);
				this.finish();
			} else {
				AppUtil.showShortMessage(this, R.string.step_3_not_equal);
				return;
			}
			break;

		}
		step++;
		showTextAndView();
	}

	/**
	 * 监听安全号码选择按钮
	 * 
	 * @param view
	 */
	public void selectButtonListener(final View v) {
		DialogBuilder dialogBuilder = new DialogBuilder(this);
		dialogBuilder.setTitle(R.string.step_2_dialog_title);
		dialogBuilder.setView(R.layout.phone_numbers);
		View view = dialogBuilder.getCenterView();
		ListView listView = (ListView) view.findViewById(R.id.phone_numbers_list_view);
		HashMap<String, ArrayList<Contacts>> map = AppUtil.getContacts(this, Constants.TYPE_PHONE);
		ArrayList<Contacts> contacts = map.get(Constants.TYPE_PHONE);// 获取手机联系人
		final ArrayList<String> data = new ArrayList<String>();
		String repx = "^1[3-8]\\d{9}$";// 匹配手机号码
		Pattern pattern = Pattern.compile(repx);
		for (Contacts c : contacts) {
			String num = c.getNumber();
			if (!StringUtil.isEmpty(num)) {
				// 只显示手机号码
				num = num.replace("+86", "");
				if (pattern.matcher(num).matches()) {
					data.add(c.getDisplayName() + "\n" + num);
				}
			}
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.phone_numbers_item, R.id.phone_numbers_item_text_view, data);
		listView.setAdapter(adapter);
		final Dialog dialog = dialogBuilder.create();
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				String num = data.get(position);
				num = num.substring(num.indexOf("\n") + 1, num.length());
				if (v.getId() == R.id.step_2_select_button_1)
					inputEditText.setText(StringUtil.trim(num));
				if (v.getId() == R.id.step_2_select_button_2)
					repeatEditText.setText(StringUtil.trim(num));
				dialog.dismiss();// 隐藏对话框
			}
		});
		dialog.show();// 显示对话框
	}
}